import time

from selenium.webdriver.common.by import By

from pageobjects.BasePage import BasePage


class EditPage(BasePage):
    __INPUT_TITLE = (By.CSS_SELECTOR, 'input#title')
    __INPUT_WRITE_CONTENT = (By.CSS_SELECTOR, '.cm-header.cm-header-2')
    __INPUT_UPDATE_CONTENT = (By.CSS_SELECTOR,'.CodeMirror-activeline')
    __CLICK_SUBMIT = (By.CSS_SELECTOR, '#submit')
    def write_blog(self, title, text):
        # time.sleep(5)
        # 输入标题
        self.do_clear(self.__INPUT_TITLE)
        self.do_send_keys(self.__INPUT_TITLE, title)
        # time.sleep(5)
        # 输入内容
        self.mac_clear_send(by=self.__INPUT_WRITE_CONTENT, text=text)
        # 点击发布
        self.do_click(self.__CLICK_SUBMIT)

        from pageobjects.ListPage import ListPage
        return ListPage(self.driver)

    def update_blog(self, title=None, text=None):
        if title:
            self.do_clear(self.__INPUT_TITLE)
            self.do_send_keys(self.__INPUT_TITLE, title)
        if text:
            self.mac_clear_send(by=self.__INPUT_UPDATE_CONTENT, text=text)

        self.do_click(self.__CLICK_SUBMIT)

        from pageobjects.ListPage import ListPage
        return ListPage(self.driver)